package com.scsj.supermarket.view.activity.goodmodel;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jaiky.imagespickers.ImageConfig;
import com.jaiky.imagespickers.ImageSelector;
import com.jaiky.imagespickers.ImageSelectorActivity;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.EditCommAdapter;
import com.scsj.supermarket.bean.EditCommBean;
import com.scsj.supermarket.customview.imagespickers.GlideLoader;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;
import com.vondear.rxtools.RxPermissionsTool;

import java.util.ArrayList;
import java.util.List;


/**
 * 编辑购物评价
 */
public class GoodCommEditActivity extends BaseActivity {

    private RecyclerView edit_comm_rv;
    private List<EditCommBean> list;
    private EditCommAdapter adapter;
    private View footer;
    private ImageConfig imageConfig;
    private LinearLayout llContainer;
    private ArrayList<String> path = new ArrayList<>();
    public static final int REQUEST_CODE = 123;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_good_comm_edit);

        initToolbar();
        setTitle("评价");
        hasBack(true);

        initView();
        initListener();
        initData();

        RxPermissionsTool.
                with(this).
                addPermission(Manifest.permission.READ_EXTERNAL_STORAGE).
                addPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE).
                initPermission();

    }

    @Override
    protected void initView() {
        edit_comm_rv = findViewById(R.id.edit_comm_rv);
        edit_comm_rv.setLayoutManager(new LinearLayoutManager(this));
        edit_comm_rv.setNestedScrollingEnabled(false);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            list.add(new EditCommBean(R.mipmap.zzz_clothing, "清涼夏日舒适夏凉被", 5));
        }
        adapter = new EditCommAdapter(list);
        edit_comm_rv.setAdapter(adapter);
        setFooterView(edit_comm_rv);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                view.findViewById(R.id.images_pickers).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        getPics();
                    }
                });
            }
        });
    }

    private void setFooterView(RecyclerView view) {
        footer = LayoutInflater.from(this).inflate(R.layout.edit_my_comm_footer_layout, view, false);
        LinearLayout comm_edit_footer_ll = footer.findViewById(R.id.comm_edit_footer_ll);
        adapter.setFooterView(footer);
    }

    public void getPics() {
        imageConfig = new ImageConfig.Builder(
                new GlideLoader())
                .steepToolBarColor(getResources().getColor(R.color.titleBlue))
                .titleBgColor(getResources().getColor(R.color.titleBlue))
                .titleSubmitTextColor(getResources().getColor(R.color.white))
                .titleTextColor(getResources().getColor(R.color.white))
                // 开启多选   （默认为多选）
                .mutiSelect()
                // 多选时的最大数量   （默认 9 张）
                .mutiSelectMaxSize(9)
                //设置图片显示容器，参数：、（容器，每行显示数量，是否可删除）
                .setContainer(llContainer, 4, true)
                // 已选择的图片路径
                .pathList(path)
                // 拍照后存放的图片路径（默认 /temp/picture）
                .filePath("/temp")
                // 开启拍照功能 （默认关闭）
                .showCamera()
                .requestCode(REQUEST_CODE)
                .build();
        ImageSelector.open(this, imageConfig);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) {
            List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);

            path.clear();
            path.addAll(pathList);
        }
    }
}
